package com.example.viewtest.draw_6;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.viewtest.Utils;

/**
 * 圆形进度条
 */
public class SpeedView extends View {

    private static final float PAINDING = Utils.db2px(20);
    private static final float STROKE_WIDTH = Utils.db2px(100);
    private static final float FRAME_WIDTH = Utils.db2px(20);

    private RectF rectF = new RectF();
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public SpeedView(Context context) {
        super(context);
    }

    public SpeedView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SpeedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        rectF.set(0, 0, getMeasuredWidth(), getMeasuredWidth());
    }

    {
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(FRAME_WIDTH);
        paint.setStrokeCap(Paint.Cap.ROUND);

        textPaint.setTextSize(STROKE_WIDTH);
        textPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(rectF.centerX(), rectF.centerY(), (rectF.width() - FRAME_WIDTH) / 2 - PAINDING, paint);

        paint.setColor(Color.GRAY);
        float offest = PAINDING + FRAME_WIDTH / 2;
        rectF.inset(offest, offest);
        canvas.drawArc(rectF, -90, 210, false, paint);
        rectF.inset(-offest, -offest);

        String text = "123456";
        Rect rect = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), rect);
        float width = textPaint.measureText(text);
        canvas.drawText(text, rectF.centerX() - width / 2, rectF.centerY() + rect.height() / 2, textPaint);
    }
}
